package cn.com.huzo.webapp.controller.trade;

import cn.com.huzo.model.entities.trade.InspectionJournalHeaderEntity;
import cn.com.huzo.model.entities.trade.InspectionJournalLinesCertificationHeaderEntity;
import cn.com.huzo.model.entities.trade.InspectionJournalLinesCertificationLinesEntity;
import cn.com.huzo.model.entities.trade.InspectionJournalLinesNoOfStyleEntity;
import cn.com.huzo.model.entities.trade.InspectionJournalLinesMaterialEntity;
import cn.com.huzo.service.trade.InspectionJournalHeaderManager;
import cn.com.huzo.service.trade.InspectionJournalLinesCertificationHeaderManager;
import cn.com.huzo.service.trade.InspectionJournalLinesCertificationLinesManager;
import cn.com.huzo.service.trade.InspectionJournalLinesMaterialManager;
import cn.com.huzo.service.trade.InspectionJournalLinesNoOfStyleManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 化检日记帐画面控制器
 *
 * @author 吴旭
 */
@Controller
public class InspectionJournalController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String _resourceNo = "0109";
    @Resource(name = "InspectionJournalHeaderManager")
    private InspectionJournalHeaderManager _manager_header;

    @Resource(name = "InspectionJournalLinesNoOfStyleManager")
    private InspectionJournalLinesNoOfStyleManager _manager_lines_noofstyle;

    @Resource(name = "InspectionJournalLinesMaterialManager")
    private InspectionJournalLinesMaterialManager _manager_lines_material;

    @Resource(name = "InspectionJournalLinesCertificationHeaderManager")
    private InspectionJournalLinesCertificationHeaderManager _manager_lines_certification_header;

    @Resource(name = "InspectionJournalLinesCertificationLinesManager")
    private InspectionJournalLinesCertificationLinesManager _manager_lines_certification_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到化检日记帐一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("trade/inspection_journal/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        // 设置功能号
        request.setAttribute("ResourceNo", _resourceNo);
        // 设置角色号
        request.setAttribute("RoleNo", _resourceNo);

        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/inspection_journal_list");
        return view;
    }

    /**
     * 跳转化检日记帐编辑画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("trade/inspection_journal/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request) throws Exception {
        // 设置功能号
        request.setAttribute("ResourceNo", _resourceNo);
        // 设置角色号
        request.setAttribute("RoleNo", _resourceNo);

        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/inspection_journal_edit");
        return view;
    }

    /**
     * 化检日记帐明细资材增加弹出画面
     * <p>
     * @return
     */
    @RequestMapping("popup/trade/inspection_journal/lines/material/rows_choice.html")
    public ModelAndView redirectLinesMaterialAddWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/inspection_journal_lines_material_rows_choice");
        return view;
    }

    /**
     * 化检日记帐明细合格证明细增加的弹出画面
     * <p>
     * @return
     */
    @RequestMapping("popup/trade/inspection_journal/certification/lines/rows_choice.html")
    public ModelAndView redirectLinesCertificationAddWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/inspection_journal_lines_certification_lines_rows_choice");
        return view;
    }

    /**
     * 化检日记帐明细合格证增加画面
     * <p>
     * @return
     */
    @RequestMapping("popup/trade/inspection_journal/lines/certification/edit.html")
    public ModelAndView redirectNewLinesWizardWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/inspection_journal_certification_edit");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/header/fetchNewNoOfBill.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewNoOfBill() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("NoOfBill", _manager_header.fetchNewNoOfBill());
        return rtnVal;
    }

    /**
     * 根据ID取得化检日记账数据。
     *
     * @param headerKey 化检日记账ID
     * @return 化检日记账数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Long headerKey) throws Exception {
        return _manager_header.fetchByHeaderKey(headerKey);
    }

    /**
     * 根据条件取得化检日记账信息分页数据。
     * <p>
     * @param filters 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> filters) throws Exception {
        return _manager_header.fetchForPage(filters);
    }

    /**
     * 保存化检日记账信息数据。
     * <p>
     * @param instance 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody InspectionJournalHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {//新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除化检日记账信息。
     *
     * @param instance 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody InspectionJournalHeaderEntity instance) throws Exception {
        return _manager_header.delete(instance);
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="款号明细处理部分">
    /**
     * 根据主记录ID全部款号明细数据列表
     *
     * @param headerKey 主表标识
     * @return 全部款号明细数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/fetchByHeaderKey.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    Map<String, Object> fetchNoOfStyleByHeaderKey(@RequestBody Long headerKey) throws Exception {
        return _manager_lines_noofstyle.fetchByHeaderKey(headerKey);
    }

    /**
     * 新增化检日记账明细信息数据（2个明细）。
     * <p>
     * @param data 要更新的数据(主表标识、产品标识列表）。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesNoOfStyleEntity> insertNoOfStyle(@RequestBody Map<String, Object> data) throws Exception {
        return _manager_lines_noofstyle.insert(data);
    }

    /**
     * 更新化检日记账款号明细信息数据。
     * <p>
     * @param dataList 要更新的数据。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateNoOfStyle(@RequestBody List<InspectionJournalLinesNoOfStyleEntity> dataList) throws Exception {
        return _manager_lines_noofstyle.update(dataList);
    }

    /**
     * 删除化检日记账明细信息。
     *
     * @param instance
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteNoOfStyle(@RequestBody InspectionJournalLinesNoOfStyleEntity instance) throws Exception {
        return _manager_lines_noofstyle.deleteLines(instance);
    }

    /**
     * 批量 删除化检日记账明细信息。
     *
     * @param dataList 要删除的数据
     * @return 款号明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/batchDelete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesNoOfStyleEntity> batchDeleteNoOfStyle(@RequestBody List<InspectionJournalLinesNoOfStyleEntity> dataList) throws Exception {
        return _manager_lines_noofstyle.batchDeleteLines(dataList);
    }

    /**
     * 根据选择的款号明细取得对应的资材列表
     *
     * @param dataList 选定的款号数据
     * @return 款明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/noofstyle/fetchMaterialByNoOfStyle.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesMaterialEntity> fetchMaterialByNoOfStyle(@RequestBody List<InspectionJournalLinesMaterialEntity> dataList) throws Exception {
        return null; //_manager_lines_noofstyle.fetchMaterialByNoOfStyle(dataList);

    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="资材明细处理部分">
    /**
     * 根据ID取得全部资材明细数据（用于增加合格证明细）。
     *
     * @param headerKey 化检日记账ID
     * @return 数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/material/fetchByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesMaterialEntity> fetchMaterialByHeaderKey(@RequestBody final Long headerKey) throws Exception {
        return _manager_lines_material.fetchByHeaderKey(headerKey);
    }

    /**
     * 根据主记录ID和所选择的款号，得到对应资材明细数据列表
     *
     * @param matchValue 条件
     * @return 款号对应资材明细数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "trade/inspection_journal/lines/material/fetchByNoOfStyle.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    List<InspectionJournalLinesMaterialEntity> fetchByNoOfStyle(@RequestBody Map<String, Object> matchValue) throws Exception {
        return _manager_lines_material.fetchByNoOfStyle(matchValue);
    }

    /**
     * 根据选择的款号，得到增加资材弹出窗口中的明细数据列表
     *
     * @param matchValue 条件
     * @return 资材明细数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "trade/inspection_journal/lines/material/fetchSelectedDataByNoOfStyle.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesMaterialEntity> fetchSelectedDataByNoOfStyle(@RequestBody Map<String, Object> matchValue) throws Exception {
        return _manager_lines_material.fetchMaterialByNoOfStyle(matchValue);
    }

    /**
     * 新增化检日记账明细信息数据（2个明细）。
     * <p>
     * @param dataList 要更新的数据。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/material/add.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer insertSelectedData(@RequestBody List<InspectionJournalLinesMaterialEntity> dataList) throws Exception {
        return _manager_lines_material.insertSelectedData(dataList);
    }

    /**
     * 更新化检日记账款号明细信息数据。
     * <p>
     * @param dataList 要更新的数据。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/material/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateMaterialLines(@RequestBody List<InspectionJournalLinesMaterialEntity> dataList) throws Exception {
        return _manager_lines_material.update(dataList);
    }

    /**
     * 删除化检日记账明细信息。
     *
     * @param instance
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/material/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer deleteMaterial(@RequestBody InspectionJournalLinesMaterialEntity instance) throws Exception {
        return _manager_lines_material.delete(instance);
    }

    /**
     * 批量 删除化检日记账明细信息，同时删除对应的资材明细记录。
     *
     * @param instance
     * @return 款号明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/material/batchDelete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer batchDeleteMaterial(@RequestBody List<InspectionJournalLinesMaterialEntity> instance) throws Exception {
        return _manager_lines_material.batchDeleteLines(instance);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="合格证主表处理部分">
    /**
     * 根据合格证标识取得合格证详细数据。
     *
     * @param linesKey 本次合格证标识
     * @return 合格证数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/header/fetchContentByLinesKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchContentByLinesKey(@RequestBody final String linesKey) throws Exception {
        return _manager_lines_certification_header.fetchContentByLinesKey(linesKey);
    }

    /**
     * 根据ID取得合格证数据。
     *
     * @param headerKey 化检日记账ID
     * @return 合格证数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/header/fetchAllByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesCertificationHeaderEntity> fetchCertificationByHeaderKey(@RequestBody final Long headerKey) throws Exception {
        return _manager_lines_certification_header.fetchCertificationByHeaderKey(headerKey);
    }

    /**
     * 保存合格证信息数据。
     * <p>
     * @param instance 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> saveCertification(@RequestBody InspectionJournalLinesCertificationHeaderEntity instance) throws Exception {
        Map<String, Object> rtnValue = new HashMap();
        String _result;

        if (instance.getLinesId() != null) {   // 编辑
            _result = _manager_lines_certification_header.update(instance).toString();
        } else {                               // 新增
            _result = _manager_lines_certification_header.insert(instance);
        }

        rtnValue.put("LinesKey", _result);
        return rtnValue;
    }

    /**
     * 删除合格证信息。
     *
     * @param instance 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesCertificationHeaderEntity> deleteCertification(@RequestBody InspectionJournalLinesCertificationHeaderEntity instance) throws Exception {
        return _manager_lines_certification_header.deleteLines(instance);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="合格证明细处理部分">
    /**
     * 新增合格证明细信息数据。
     * <p>
     * @param criterion 选择后的资材数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsertCertificationLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_certification_lines.batchInsert(criterion);
    }

    /**
     * 保存合格证明细信息数据。
     * <p>
     * @param dataList 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateCertificationLines(@RequestBody List<InspectionJournalLinesCertificationLinesEntity> dataList) throws Exception {
        return _manager_lines_certification_lines.updateLines(dataList);

    }

    /**
     * 删除合格证明细信息。
     *
     * @param instance 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "trade/inspection_journal/lines/certification/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<InspectionJournalLinesCertificationLinesEntity> deleteCertificationLines(@RequestBody InspectionJournalLinesCertificationLinesEntity instance) throws Exception {
        return _manager_lines_certification_lines.deleteLines(instance);
    }
    // </editor-fold>
}
